-- @owner: @zhanghuan96
-- @date: 2024/12/18
-- @testpoint: 自定义函数嵌套，使用gms_utility.format_error_stack函数
@conn PrimaryDbAmode;
--前置条件
drop extension if exists gms_utility;
create extension gms_utility;
--step1:创建自定义函数;expect:成功
drop function if exists func_format0010;
create or replace function func_format0010(a int, b int)
returns int as $$
declare
    res int := 0;
begin
    res = a / b;
    return res;
exception
    when others then
    raise exception 'expected exception';
end;
$$ language plpgsql;
/
--step2:创建自定义函数，嵌套上述创建的函数;expect:成功
drop function if exists func_format0010_01;
create or replace function func_format0010_01(a int, b int)
returns int as $$
declare
    res int := 0;
    v_call_stack TEXT;
begin
    res := func_format0010(a, b);
    return res;
exception
    when others then
      v_call_stack := gms_utility.format_call_stack();
      RAISE INFO 'INFO:Call Stack: %', v_call_stack;
    return -1;
end;
$$ language plpgsql;
/
--step3:调用函数;expect:成功
select func_format0010_01(100, 2);
--step4:调用函数，代码发生异常使用gms_utility.format_call_stack;expect:成功，打印堆栈信息
select func_format0010_01(100, 0);
--step5:清理环境;expect:成功
drop function if exists func_format0010;
drop function if exists func_format0010_01;
drop extension if exists gms_utility;